package com.founder.barcode.bussiness.web;

import java.sql.SQLException;

import org.junit.Assert;

import com.founder.barcode.bussiness.code.service.CodeCheckLogService;
import com.founder.barcode.bussiness.util.BaseRequestService;
import com.founder.barcode.bussiness.util.SpringBussUtil;
import com.founder.barcode.bussiness.xml.XmlParserUtil;
import com.founder.barcode.persistence.bussiness.po.QrCodeInfoPO;

/**
 * 验码部分的公共逻辑
 * @author hucw
 *
 */
public abstract class BaseValidate extends BaseRequestService {

    protected String transactionId = null;
    
    public String getTransactionId() {
        return transactionId;
    }
    
    /**
     * 模拟保存验码信息
     * @param contentType
     * @param content
     * @throws SQLException
     */
    protected void saveValidate(String contentType,String content,String userId) throws SQLException {
        SpringBussUtil springBussUtil = SpringBussUtil.getInstance();
        CodeCheckLogService codeCheckLogService = springBussUtil.getBean("codeCheckLogService", CodeCheckLogService.class);
        QrCodeInfoPO qr = codeCheckLogService.getCodeDetailInfoByCode(content, contentType);
        transactionId = buildXml.genTransactionId(qr.getBusinessId(), null);
        codeCheckLogService.save(contentType, content, transactionId, userId, 0);
    }
    
    /**
     * 模拟调用验码接口，9997表示网络异常，应用平台已经调用到,认为验码成功
     * @param result
     */
    public void validateSuccess(String result) {
        String code = XmlParserUtil.getXMLValue(result, "Code");
        Assert.assertTrue("1000".equals(code) || "9997".equals(code));
    }
}
